<template>
  <div class="wrap">
    <el-dialog
      title="供应商信息"
      :visible.sync="dialogVisible"
      width="65%"
      :showClose="false"
    >
      <!-- <span>这是一段信息</span> -->
      <span class="cancel" @click="supplierCancel" style="cursor: pointer">X</span>
      <div class="supplier-wrap">
        <el-form
          :inline="true"
          :model="dataForm"
          @keyup.enter.native="getDataList()"
        >
          <el-form-item>
            <div>名称：</div>
            <el-input
              v-model="dataForm.supplier"
              placeholder="名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div>手机号：</div>
            <el-input
              v-model="dataForm.phone"
              placeholder="手机号"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div>联系电话：</div>
            <el-input
              v-model="dataForm.number"
              placeholder="联系电话"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div>备注：</div>
            <el-input
              v-model="dataForm.remark"
              placeholder="备注"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div class="block"></div>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
            <el-button @click="resetDataList()">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @row-click="singleElection"
          style="width: 100%"
        >
          <el-table-column label="" width="65">
            <template slot-scope="scope">
              <el-radio
                class="radio"
                v-model="templateSelection"
                :label="scope.$index"
                >&nbsp;</el-radio
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="supplier"
            header-align="center"
            align="center"
            width="80"
            label="名称"
          >
          </el-table-column>
          <el-table-column
            prop="contacts"
            header-align="center"
            align="center"
            label="联系人"
          >
          </el-table-column>
          <el-table-column
            prop="telephone"
            header-align="center"
            align="center"
            label="手机号码"
          >
          </el-table-column>
          <el-table-column
            prop="email"
            header-align="center"
            align="center"
            width="180"
            label="电子邮箱"
          >
          </el-table-column>
          <el-table-column
            prop="phoneNum"
            header-align="center"
            align="center"
            label="联系电话"
          >
          </el-table-column>
          <el-table-column
            prop="fax"
            header-align="center"
            align="center"
            label="传真"
          >
          </el-table-column>
          <el-table-column
            prop="advanceIn"
            header-align="center"
            align="center"
            label="预付款"
          >
          </el-table-column>
          <el-table-column
            prop="beginNeedGet"
            header-align="center"
            align="center"
            label="初期应收"
          >
          </el-table-column>
          <el-table-column
            prop="beginNeedPay"
            header-align="center"
            align="center"
            label="初期应付"
          >
          </el-table-column>
          <el-table-column
            prop="allNeedGet"
            header-align="center"
            align="center"
            label="末期应收"
          >
          </el-table-column>
          <el-table-column
            prop="allNeedPay"
            header-align="center"
            align="center"
            label="末期应付"
          >
          </el-table-column>
          <el-table-column
            prop="taxRate"
            header-align="center"
            align="center"
            label="税率"
          >
          </el-table-column>
        </el-table>
        <p class="btn">
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="supplierSure">确 定</el-button>
            <el-button @click="supplierCancel">取 消</el-button>
          </span>
        </p>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper"
          class="page"
        >
        </el-pagination>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  props: ["isfind"], // 父组件给子组件传递值的时候就需要通过props去传递
  data() {
    return {
      dialogVisible: true,
      dataForm: {
        number: "",
        phone: "",
        name: "",
        remark: "",
        supplier: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      templateSelection: "",
    };
  },
  created() {
    this.getDataList();
    this.getCategory();
  },
  activated() {
    this.getDataList();
    this.getCategory();
  },
  methods: {
    // 确定按钮
    supplierSure() {
      if (typeof this.templateSelection == "string") {
        this.$message({
          message: "请选中对应的供应商",
          type: "warning",
          duration: 1500,
        });
        return;
      }
      this.dialogVisible = false;
      console.log(this.supplierinfo);
      this.$emit("getSupplier", this.supplierinfo);
      this.$emit("changeSupplier", this.dialogVisible);
    },
    // 取消按钮
    supplierCancel() {
      // console.log(this.isfind)
      // return
      this.dialogVisible = false;
      this.$emit("changeSupplier", this.dialogVisible);
    },
    // 选中当前行
    singleElection(row) {
      console.log(row);
      // return;
      this.supplierinfo = row;
      // this.templateRadio = row.id;
    },
    // 选中分类获取下拉状态
    handlechange() {
      console.log(this.$refs["refSubCat"].getCheckedNodes()[0].value);
      this.categoryIds = this.$refs["refSubCat"].getCheckedNodes()[0].value;
    },
    // 下拉
    checked(e) {
      if (e) {
        this.getCategory();
      }
    },
    // 选中
    change(id) {
      console.log(id);
      // this.dataForm.id = id;
    },
    // 获取分类的下拉框
    getCategory() {
      this.$http({
        url: this.$http.adornUrl(
          "/materialCategory/getMaterialCategoryTree?id="
        ),
        method: "post",
      }).then(({ data }) => {
        console.log("成功获取到菜单数据...", data.data);
        this.options = this.getTreeData(data.data);
      });
    },
    // 递归判断列表，把最后的children设为undefined
    getTreeData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          data[i].children = undefined;
        } else {
          this.getTreeData(data[i].children);
        }
      }
      return data;
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let type = "供应商";
      let str = `{"supplier":"${this.dataForm.supplier}","type":"${type}","phonenum":"${this.dataForm.number}","telephone":"${this.dataForm.phone}","description":"${this.dataForm.remark}"}`;
      var search = encodeURIComponent(str);
      this.$http({
        url: this.$http.adornUrl(`/supplier/list?search=${search}`),
        method: "get",
        params: this.$http.adornParams({
          currentPage: this.pageIndex,
          pageSize: this.pageSize,
        }),
      }).then(({ data }) => {
        // console.log(data.data);
        if (data && data.code === 0) {
          this.dataList = data.data;
          this.totalPage = data.data.length;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 更新状态
    updateMemberStatus(data) {
      console.log("最新信息", data);
      let { id, status } = data;
      //发送请求修改状态
      this.$http({
        url: this.$http.adornUrl("/member/member/update/status"),
        method: "post",
        data: this.$http.adornData({ id, status }, false),
      }).then(({ data }) => {
        this.$message({
          type: "success",
          message: "状态更新成功",
        });
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 重置
    resetDataList() {
      // console.log(11)
      this.dataForm.model = "";
      this.dataForm.category = "";
      this.dataForm.barcode = "";
      this.dataForm.name = "";
      this.dataForm.specifications = "";
      this.value1 = "";
      this.categoryIds = "";
      this.dataForm.categoryId = "";
      this.getDataList();
    },
    downLoadFiles(url, params) {
      return new Promise((resolve, reject) => {
        axios
          .post(this.$http.adornUrl(url), params, {
            responseType: "blob",
            headers: {
              "Content-Type": "application/json; application/octet-stream",
            },
          })
          .then((res) => {
            if (!res) {
              Message.warning("导出失败");
              return;
            }
            resolve(res);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
  },
};
</script>
<style scoped>
.el-form-item .el-form-item__content div {
  display: inline-block;
}
.el-input {
  position: relative;
  font-size: 0.072917rem;
  display: inline-block;
  width: 150px;
}
.cancel {
  position: absolute;
  right: 20px;
  top: 18px;
}
.wrap >>> .el-dialog__header {
  text-align: center;
  background: #e6f5fe;
  font-size: 16px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #31333d;
}
.wrap .btn {
  text-align: right;
}
.el-button {
  width: 80px;
  height: 32px;
}
.page {
  text-align: center;
}
.wrap >>> .has-gutter th {
  background: #dddddd;
}
</style>
